//
//  HXRelatedFoodVC.m
//  ZCooking
//
//  Created by 韩旭 on 16/9/16.
//  Copyright © 2016年 HX_. All rights reserved.
//

#import "HXRelatedFoodVC.h"
#import "HXRelatedFoodTCell.h"
#import "HX_Macro.h"
#import "HXRelatedFoodModel.h"
#import "HXRelatedBigModel.h"
@interface HXRelatedFoodVC ()<UITableViewDataSource, UITableViewDelegate>
@property (nonatomic, strong) UITableView *tableView;
/**
 *  页数
 */
@property (nonatomic, assign) NSInteger pageCount;
@end

@implementation HXRelatedFoodVC

- (NSMutableArray *)arrTagsinfo {
    if (!_arrTagsinfo) {
        _arrTagsinfo = [NSMutableArray array];
    }
    return _arrTagsinfo;
}
- (UITableView *)tableView {
    if (!_tableView) {
        self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT - 250) style:0];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        [self.view addSubview:_tableView];
        [_tableView registerNib:[UINib nibWithNibName:@"HXRelatedFoodTCell" bundle:[NSBundle mainBundle]] forCellReuseIdentifier:@"HXRelatedFoodTCell"];
        self.tableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(addNetWorking)];
    }
    return _tableView;
}
- (void)addNetWorking {
    self.pageCount += 1;
    NSString *url = @"http://api.izhangchu.com/";
    NSString *body = [NSString stringWithFormat:@"user_id=1666223&material_id=%@&page=%ld&token=3C6CB2536B8DE42A4070A68388EA5F99&methodName=MaterialDishes&size=20&version=4.40&", self.userId,self.pageCount];
    NSDictionary *dicHeader = @{@"Content-Type" : @"application/x-www-form-urlencoded; charset=UTF-8"};
    [HXNetTool postNetWithUrl:url body:body BodyStyle:HXBodyString Header:dicHeader Response:HXJSON Success:^(id result) {
        NSDictionary *results = result;
        NSDictionary *dicData = results[@"data"];
        NSArray *arrData = dicData[@"data"];
        for (NSDictionary *dicAll in arrData) {
            HXRelatedFoodModel *foodModel = [HXRelatedFoodModel ModelWithDic:dicAll];
            [self.arrTagsinfo addObject:foodModel];
        }
        [_tableView reloadData];
        [self.tableView.mj_footer endRefreshing];
    } Failure:^(NSError *error) {
    }];
}
- (void)viewDidLoad {
    [super viewDidLoad];
     self.pageCount = 1;
    [self getNetWorkingFoodUserId:self.userId];
    [self tableView];
}
- (void)getNetWorkingFoodUserId:(NSString *)userId {
    NSString *url = @"http://api.izhangchu.com/";
    NSString *body = [NSString stringWithFormat:@"material_id=%@&page=1&methodName=MaterialDishes&size=20&version=4.40&", userId];
    NSDictionary *dicHeader = @{@"Content-Type" : @"application/x-www-form-urlencoded; charset=UTF-8"};
    [HXNetTool postNetWithUrl:url body:body BodyStyle:HXBodyString Header:dicHeader Response:HXJSON Success:^(id result) {
        NSDictionary *results = result;
        NSDictionary *dicData = results[@"data"];
        NSArray *arrData = dicData[@"data"];
        for (NSDictionary *dicAll in arrData) {
            HXRelatedFoodModel *foodModel = [HXRelatedFoodModel ModelWithDic:dicAll];
            [self.arrTagsinfo addObject:foodModel];
        }
        [_tableView reloadData];
    } Failure:^(NSError *error) {
    }];
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.arrTagsinfo.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    HXRelatedFoodTCell *cell = [tableView dequeueReusableCellWithIdentifier:@"HXRelatedFoodTCell"];
    cell.relateFood = self.arrTagsinfo[indexPath.row];
    cell.backgroundColor = [UIColor whiteColor];
    return cell;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 120;
}
/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end
